import {getUrlQueryString} from "@src/common/utils";
import base from "@src/store/base";
import member from "@src/store/member";
import {queryMemberGiveStatus, receiveMemberGive} from "@src/api/member";
import router from "@src/router";
import {storeBindingsBehavior} from "mobx-miniprogram-bindings";

Component({
  behaviors: [storeBindingsBehavior, router.routerBehavior],
  storeBindings: [{
    store: base,
    fields: ['themeColor', 'themeTitleColor', 'businessLogo', 'businessName'],
  }, {
    store: member,
    fields: ['name'],
    actions: ['getBaseInfo'],
  }],
  data: {
    isSendValid: true,
    isReceive: false,
    isExpired: false,
    hasInit: false,
    // 1.赠送人 2.领取人 3.非领取人
    receivePersonIsSelf: false,
    queryPageData: null,
    category: null,
    rechargeAward: null
  },
  methods: {
    onLoad(options) {
      let scene = "", receiveWay = "";
      if (options.scene) {
        scene = decodeURIComponent(options.scene);
      } else if (options.path) {
        scene = getUrlQueryString(options.path, "scene")
      }
      if (options.receiveWay) {
        receiveWay = options.receiveWay;
      }
      this.setData({scene, receiveWay})
    },
    onLogin() {
      this.fetchData();
    },
    onPullDownRefresh() {
      wx.stopPullDownRefresh();
      this.fetchData();
    },
    fetchData() {
      let data = {
        receiveCode: this.data.scene
      }
      queryMemberGiveStatus(data).then(res => {
        if (res.code === 0 && res.data.code !== 101) {
          let queryPageData = res.data.info;
          let category = res.data.info.category;
          let rechargeAward = res.data.info.rechargeAward;
          this.setData({queryPageData, category, rechargeAward})
          this.parseData(res.data);
        } else {
          let noValidTitle = res.data.msg; //标题
          this.setData({isSendValid: false, noValidTitle, hasInit: true})
        }
      }).catch(()=> {
        this.setData({isSendValid: false, noValidTitle: "异常数据", hasInit: true})
      })
    },
    handleToHome() {
      router.push({name: "index"})
    },
    handleGetPhoneNumber(e) {
      if (!e.detail.encryptedData) {
        return;
      }
      let data = {
        ivStr: e.detail.iv,
        encryptedData: e.detail.encryptedData,
      }
      this.handleReceive(data)
    },
    handleReceive(phoneData) {
      wx.showLoading({title: "领取中...", mask: true});
      let data = {
        code: this.data.scene,
        receiveWay: this.data.receiveWay,
        encryptedData: phoneData.encryptedData,
        ivStr: phoneData.ivStr
      }
      receiveMemberGive(data).then(res => {
        wx.hideLoading();
        if (res.code === 0) {
          this.getBaseInfo();
          wx.showModal({
            title: "提示",
            content: `恭喜您已成功领取${this.data.category.categoryName}会员`,
            showCancel: false,
            confirmText: "去首页",
            success: sm => {
              if (sm.confirm) {
                router.push({name: "index"})
              }
            }
          });
        } else {
          wx.showToast({title: res.msg, icon: "none", duration: 3000});
        }
      });
    },
    parseData(giveInfo) {
      if (giveInfo.isGiver) {
        // 跳转到赠送页
        router.redirectTo({
          name: "member/sendGiftDetail",
          params: {receiveCode: this.data.scene}
        })
        return;
      }
      if (giveInfo.code === 103 || giveInfo.code === 104) {
        this.setData({isExpired: true, hasInit: true})
      } else if (giveInfo.code === 102) {
        let receivePersonIsSelf = giveInfo.isReceiver ? 2 : 3;
        this.setData({isReceive: true, receivePersonIsSelf, isExpired: false, hasInit: true})
      } else {
        this.setData({isReceive: false, receivePersonIsSelf: 2, isExpired: false, hasInit: true})
      }
    }
  }
})
